
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的博客 - 首页</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f7f7f7;
            color: #333;
        }
        .header {
            background-color: #ffffff;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            padding: 15px 0;
        }
        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
        }
        .header h1 {
            margin: 0;
            font-size: 1.8rem;
            color: #333;
        }
        .action-buttons a {
            margin-left: 15px;
            text-decoration: none;
            padding: 8px 16px;
            border-radius: 4px;
            font-weight: 500;
            font-size: 14px;
            transition: all 0.2s;
        }
        .new-btn {
            background-color: #0f9d58;
            color: white !important;
        }
        .new-btn:hover {
            background-color: #0b8043;
        }
        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }
        .welcome {
            background-color: #fff;
            padding: 30px;
            border-radius: 5px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-bottom: 30px;
            text-align: center;
        }
        .welcome h2 {
            margin-top: 0;
            color: #333;
            font-size: 1.8rem;
        }
        .welcome p {
            color: #666;
            font-size: 1.1rem;
            max-width: 800px;
            margin: 15px auto;
            line-height: 1.6;
        }
        .article-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }
        .article-card {
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            padding: 20px;
            transition: transform 0.2s, box-shadow 0.2s;
        }
        .article-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .article-card h3 {
            margin-top: 0;
            margin-bottom: 10px;
            font-size: 1.3rem;
        }
        .article-card h3 a {
            color: #333;
            text-decoration: none;
        }
        .article-card h3 a:hover {
            color: #4285f4;
        }
        .article-meta {
            color: #666;
            font-size: 14px;
            margin-bottom: 15px;
        }
        .article-actions {
            margin-top: 15px;
            display: flex;
            gap: 10px;
        }
        .article-actions a {
            padding: 6px 12px;
            font-size: 14px;
            border-radius: 4px;
            text-decoration: none;
        }
        .view-action {
            background-color: #4285f4;
            color: white;
        }
        .edit-action {
            background-color: #f1f3f4;
            color: #333;
        }
        .no-articles {
            background-color: #fff;
            padding: 30px;
            border-radius: 5px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            text-align: center;
            color: #666;
        }
        /* 响应式设计 */
        @media (max-width: 768px) {
            .article-list {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <h1>我的博客</h1>
            <div class="action-buttons">
                <a href="/edit/new.md" class="new-btn">写新文章</a>
            </div>
        </div>
    </div>

    <div class="container">
        <div class="welcome">
            <h2>欢迎来到我的Markdown博客</h2>
            <p>这是一个基于Flask和Vditor打造的轻量级Markdown博客系统，您可以在这里阅读和编辑Markdown文章。</p>
        </div>

        <div id="article-container">
            <!-- 文章列表将通过JavaScript动态加载 -->
            <div class="loading" id="loading">加载中...</div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const articleContainer = document.getElementById('article-container');

            // 加载所有文章列表
            fetch('/api/list_articles')
                .then(response => {
                    if (!response.ok) {
                        throw new Error('获取文章列表失败');
                    }
                    return response.json();
                })
                .then(articles => {
                    articleContainer.innerHTML = ''; // 清除加载提示

                    if (articles.length === 0) {
                        // 没有文章
                        articleContainer.innerHTML = `
                            <div class="no-articles">
                                <h3>暂无文章</h3>
                                <p>点击右上角的"写新文章"按钮开始创作吧！</p>
                            </div>
                        `;
                        return;
                    }

                    // 创建文章列表
                    const articleList = document.createElement('div');
                    articleList.className = 'article-list';

                    articles.forEach(article => {
                        const articleCard = document.createElement('div');
                        articleCard.className = 'article-card';

                        articleCard.innerHTML = `
                            <h3><a href="/view/${article.filename}">${article.title}</a></h3>
                            <div class="article-meta">最后修改：${article.modified}</div>
                            <div class="article-actions">
                                <a href="/view/${article.filename}" class="view-action">阅读</a>
                                <a href="/edit/${article.filename}" class="edit-action">编辑</a>
                            </div>
                        `;

                        articleList.appendChild(articleCard);
                    });

                    articleContainer.appendChild(articleList);
                })
                .catch(error => {
                    console.error('Error:', error);
                    articleContainer.innerHTML = `
                        <div class="no-articles">
                            <h3>加载失败</h3>
                            <p>无法加载文章列表: ${error.message}</p>
                        </div>
                    `;
                });
        });
    </script>
</body>
</html>

